<template>
<div class="login">
    <!-- 
        1111 
        潜规则：你可以给每个elementui标签加class然后加样式
        但是：不推荐，因为在elementui标签 本身就是class名
        举例：el-form标签 就相当于 有一个 class="el-form"
    -->
    <!-- el-form -->
    <!-- <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm"> -->
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <!-- <el-form-item label="密码" prop="pass"> -->
        <el-form-item prop="pass">
            <el-input type="text" prefix-icon="el-icon-s-custom" clearable v-model="ruleForm.pass" placeholder="请输入用户名" autocomplete="off"></el-input>
        </el-form-item>
        <!-- <el-form-item label="确认密码" prop="checkPass"> -->
        <el-form-item prop="checkPass">
            <el-input type="password" prefix-icon="el-icon-lock" clearable show-password v-model="ruleForm.checkPass" placeholder="请输入密码" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
        </el-form-item>
    </el-form>
    <!-- /el-form -->
</div>
</template>
<script>
export default {
data() {
    var checkAge = (rule, value, callback) => {
    if (!value) {
        return callback(new Error('年龄不能为空'));
    }
    setTimeout(() => {
        if (!Number.isInteger(value)) {
        callback(new Error('请输入数字值'));
        } else {
        if (value < 18) {
            callback(new Error('必须年满18岁'));
        } else {
            callback();
        }
        }
    }, 1000);
    };
    var validatePass = (rule, value, callback) => {
    if (value === '') {
        callback(new Error('请输入密码'));
    } else {
        if (this.ruleForm.checkPass !== '') {
        this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
    }
    };
    var validatePass2 = (rule, value, callback) => {
    if (value === '') {
        callback(new Error('请再次输入密码'));
    } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
    } else {
        callback();
    }
    };
    return {
    ruleForm: {
        pass: '',
        checkPass: '',
        age: ''
    },
    rules: {
        pass: [
        { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
        { validator: validatePass2, trigger: 'blur' }
        ],
        age: [
        { validator: checkAge, trigger: 'blur' }
        ]
    }
    };
},
methods: {
    submitForm(formName) {
    this.$refs[formName].validate((valid) => {
        if (valid) {
        alert('submit!');
        } else {
        console.log('error submit!!');
        return false;
        }
    });
    },
    resetForm(formName) {
    this.$refs[formName].resetFields();
    }
}
}
</script>
<style lang="scss" scoped>
// lang="scss"  style标签中就可以用scss语法
// scoped 样式仅当前组件/页面生效，不会全局污染

// 登录组件根
.login {
    width: 100%;
    height: 100%;
    background: #2d3a4b;
    display: flex;
    justify-content: center;
    align-items: center;

    // el-form
    .el-form {
        width: 220px;
        // background: red; // 调试 临时加 不推荐border 因为占用像素
    }

    // el-button
    .el-button {width: 100%;}
}
</style>